﻿namespace WMS6.Web.OrderManagement
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using WMS6.Web;
    using WMS6.Web.BaseInfoServiceRef;
    using WMS6.Web.OrderMgtServiceRef;
    using WMS6.Web.UserControls;

    public class PurchaseOrderList : BasePage
    {
        protected Button btnAction;
        protected Button btnDelete;
        protected Button btnEdit;
        protected Button btnExport;
        protected Button btnFinish;
        protected Button btnList;
        protected Button btnNew;
        protected Button btnRefresh;
        protected Button btnReport;
        protected Button btnReSearch;
        protected Button btnSearch;
        protected HtmlForm F3012;
        protected GridView gvPurchaseOrder;
        protected HtmlInputHidden hdnActionID;
        protected HtmlInputHidden hdnFrom;
        protected HtmlInputHidden hdnRtnValue;
        protected Label lblMsg;
        protected Label lblUom;
        protected HtmlLink mycss;
        protected Pager PagerFooter;
        protected ScriptManager ScriptManager1;
        protected UpdatePanel up1;

        private void BindData2PurchaseOrderList(PurchaseOrderQueryEntity purchaseOrderQuery)
        {
            this.PagerFooter.CurrentPage = int.Parse(purchaseOrderQuery.CurrentPage);
            DataSet ds = new DataSet();
            ds = OrderMgtServiceClientProxy.GetInstance().GetPurchaseOrderInfoByQueryEntity(purchaseOrderQuery);
            base.BindData2GridView(this.gvPurchaseOrder, ds);
            if ((ds != null) && (ds.Tables.Count > 1))
            {
                this.PagerFooter.PageInfo = ds.Tables[1].Rows[0]["CurrentPage"].ToString() + " / " + ds.Tables[1].Rows[0]["TotalPagesCount"].ToString() + "&nbsp;&nbsp;&nbsp;共: " + ds.Tables[1].Rows[0]["TotalRecordsCount"].ToString();
                this.PagerFooter.TotalPages = int.Parse(ds.Tables[1].Rows[0]["TotalPagesCount"].ToString());
            }
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                int i;
                List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPurchaseOrder, "PO_ID");
                List<string> listStatus = base.GetSelectedIDsInGridView(this.gvPurchaseOrder, "STATUS");
                List<string> listCreatedBy = base.GetSelectedIDsInGridView(this.gvPurchaseOrder, "CREATED_BY");
                bool isPSupervisor = false;
                if ((base.UserInfo.UserRoleInfos != null) && (base.UserInfo.UserRoleInfos.Count > 0))
                {
                    for (int n = 0; base.UserInfo.UserRoleInfos.Count > n; n++)
                    {
                        if (base.UserInfo.UserRoleInfos[n].RoleID == "PSupervisor")
                        {
                            isPSupervisor = true;
                            break;
                        }
                    }
                }
                if (!isPSupervisor && ((listCreatedBy != null) || (listCreatedBy.Count != 0)))
                {
                    for (i = 0; i < listCreatedBy.Count; i++)
                    {
                        if (listCreatedBy[i].ToString() != base.UserInfo.UserID)
                        {
                            this.lblMsg.Text = " 无权限删除单据 " + selectedID[i];
                            return;
                        }
                    }
                }
                if ((listStatus != null) || (listStatus.Count != 0))
                {
                    for (i = 0; i < listStatus.Count; i++)
                    {
                        if (listStatus[i].ToString().ToUpper() != "NEW")
                        {
                            this.lblMsg.Text = base.GetGlobalMsgResource("F3100_006");
                            return;
                        }
                    }
                }
                if ((selectedID == null) || (selectedID.Count == 0))
                {
                    this.lblMsg.Text = base.GetGlobalMsgResource("C008");
                }
                else
                {
                    this.lblMsg.Text = OrderMgtServiceClientProxy.GetInstance().DeletePurchaseOrderInfo(selectedID) + base.GetGlobalMsgResource("C006");
                    PurchaseOrderQueryEntity purchaseOrderQuery = this.GetPurchaseOrderQueryEntity();
                    this.BindData2PurchaseOrderList(purchaseOrderQuery);
                }
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C007") + ex.Message;
            }
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPurchaseOrder, "PO_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                base.Response.Redirect(string.Format("PurchaseOrder.aspx?from=list&ID={0}", base.UrlEncode(selectedID[0])));
            }
        }

        protected void btnExport_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQuery = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQuery.IsGetAll = true;
            this.hdnActionID.Value = "Y";
            string currentPageIndex = purchaseOrderQuery.CurrentPage;
            purchaseOrderQuery.CurrentPage = "0";
            this.gvPurchaseOrder.Columns[1].Visible = false;
            this.gvPurchaseOrder.AllowSorting = false;
            this.BindData2PurchaseOrderList(purchaseOrderQuery);
            this.gvPurchaseOrder.ShowFooter = false;
            purchaseOrderQuery.IsGetAll = false;
            purchaseOrderQuery.CurrentPage = currentPageIndex;
            this.SetPurchaseOrderQueryEntity(purchaseOrderQuery);
            this.hdnActionID.Value = "";
            base.Export2Excel("PurchaseOrder.xls", this.gvPurchaseOrder);
        }

        protected void btnFinish_Click(object sender, EventArgs e)
        {
            List<string> selectedID = base.GetSelectedIDsInGridView(this.gvPurchaseOrder, "PO_ID");
            if ((selectedID == null) || (selectedID.Count == 0))
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C008");
            }
            else if (selectedID.Count > 1)
            {
                this.lblMsg.Text = base.GetGlobalMsgResource("C009");
            }
            else
            {
                this.hdnRtnValue.Value = selectedID[0];
            }
        }

        protected void btnFirst_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrder = this.GetPurchaseOrderQueryEntity();
            purchaseOrder.CurrentPage = "0";
            this.BindData2PurchaseOrderList(purchaseOrder);
        }

        protected void btnLast_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrder = this.GetPurchaseOrderQueryEntity();
            purchaseOrder.CurrentPage = (this.PagerFooter.TotalPages - 1).ToString();
            this.BindData2PurchaseOrderList(purchaseOrder);
        }

        protected void btnList_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrderQuery = this.GetPurchaseOrderQueryEntity();
            purchaseOrderQuery.PoID = "";
            purchaseOrderQuery.Status = "";
            purchaseOrderQuery.IsActive = "";
            this.BindData2PurchaseOrderList(purchaseOrderQuery);
        }

        protected void btnNewSearch_Click(object sender, EventArgs e)
        {
            base.Server.Transfer(string.Format("PurchaseOrderQuery.aspx?action={0}", base.UrlEncode(this.hdnActionID.Value)));
        }

        protected void btnNext_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrder = this.GetPurchaseOrderQueryEntity();
            if (int.Parse(purchaseOrder.CurrentPage) < (this.PagerFooter.TotalPages - 1))
            {
                purchaseOrder.CurrentPage = (int.Parse(purchaseOrder.CurrentPage) + 1).ToString();
                this.BindData2PurchaseOrderList(purchaseOrder);
            }
        }

        protected void btnPrevious_Click(object sender, EventArgs e)
        {
            PurchaseOrderQueryEntity purchaseOrder = this.GetPurchaseOrderQueryEntity();
            if (int.Parse(purchaseOrder.CurrentPage) > 0)
            {
                purchaseOrder.CurrentPage = (int.Parse(purchaseOrder.CurrentPage) - 1).ToString();
                this.BindData2PurchaseOrderList(purchaseOrder);
            }
        }

        private PurchaseOrderQueryEntity GetPurchaseOrderQueryEntity()
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                return (base.PageQueryEntityViewState as PurchaseOrderQueryEntity);
            }
            return (base.PageQueryEntitySession as PurchaseOrderQueryEntity);
        }

        protected void GoTo_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(((TextBox) this.PagerFooter.FindControl("txtGo2Page")).Text.Trim()))
            {
                PurchaseOrderQueryEntity purchaseOrder = this.GetPurchaseOrderQueryEntity();
                purchaseOrder.CurrentPage = this.PagerFooter.GoToPage.ToString();
                this.BindData2PurchaseOrderList(purchaseOrder);
            }
        }

        protected void gvPurchaseOrder_Init(object sender, EventArgs e)
        {
            base.InitDetailGridView("F3012", this.gvPurchaseOrder);
        }

        protected void gvPurchaseOrder_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                e.Row.Cells[0].Text = (((this.PagerFooter.CurrentPage * this.PagerFooter.PageSize) + e.Row.RowIndex) + 1).ToString();
                e.Row.Attributes.Add("onmouseout", "GridViewRowMouseout(this);");
                e.Row.Attributes.Add("onmouseover", "GridViewRowMouseover(this);");
                ((CheckBox) e.Row.FindControl("chbSelect")).Attributes.Add("onclick", "GridViewCheckBoxClick('" + e.Row.ClientID + "')");
                base.SetGridViewLongTextLabel((Label) e.Row.FindControl("lblRemark"));
                if (!string.IsNullOrEmpty(this.hdnActionID.Value))
                {
                    ((HyperLink) e.Row.FindControl("HyperLink1")).Enabled = false;
                }
            }
        }

        protected void gvPurchaseOrder_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            string strValue = base.UrlReplace(this.gvPurchaseOrder.DataKeys[e.NewSelectedIndex]["PO_ID"].ToString());
            string strStatus = base.UrlReplace(this.gvPurchaseOrder.DataKeys[e.NewSelectedIndex]["STATUS"].ToString());
            if (!string.IsNullOrEmpty(strValue))
            {
                if (strStatus.ToUpper() != "CLOSED")
                {
                    string strCheck = "N";
                    SysSettingInfo sysSetting = BaseInfoServiceClientProxy.GetInstance().GetInfoByID("CheckPOIsGeneratedASN", "SYS_SETTING") as SysSettingInfo;
                    if ((sysSetting != null) && (sysSetting.Value == "Y"))
                    {
                        strCheck = InboundServiceClientProxy.GetInstance().CheckPoIsGeneratedASN(strValue);
                    }
                    ScriptManager.RegisterStartupScript(this.Page, base.GetType(), "", "<script language='javascript'> window.returnValue ='" + (strValue + "@_@" + strCheck) + "';  window.close();</script>", false);
                }
                else
                {
                    this.lblMsg.Text = "该订单已关闭！";
                }
            }
        }

        protected void gvPurchaseOrder_Sorting(object sender, GridViewSortEventArgs e)
        {
            try
            {
                PurchaseOrderQueryEntity purchaseOrderQuery = this.GetPurchaseOrderQueryEntity();
                purchaseOrderQuery.SortField = e.SortExpression;
                if (purchaseOrderQuery.SortDirection == "ASC")
                {
                    purchaseOrderQuery.SortDirection = "DESC";
                }
                else if (purchaseOrderQuery.SortDirection == "DESC")
                {
                    purchaseOrderQuery.SortDirection = "ASC";
                }
                this.BindData2PurchaseOrderList(purchaseOrderQuery);
            }
            catch (Exception ex)
            {
                this.lblMsg.Text = ex.Message;
            }
        }

        private void LoadData()
        {
            PurchaseOrderQueryEntity purchaseOrderQuery = new PurchaseOrderQueryEntity();
            if (string.IsNullOrEmpty(base.Request.QueryString["from"]))
            {
                if (this.PreviousPage != null)
                {
                    purchaseOrderQuery.PoID = this.PreviousPage.PoID;
                    purchaseOrderQuery.Status = this.PreviousPage.Status;
                    purchaseOrderQuery.OwnerID = this.PreviousPage.OwnerID;
                    purchaseOrderQuery.PoType = this.PreviousPage.PoType;
                    purchaseOrderQuery.PoDate = this.PreviousPage.PoDate;
                    purchaseOrderQuery.VendorID = this.PreviousPage.VendorID;
                    purchaseOrderQuery.VendorName = this.PreviousPage.VendorName;
                    purchaseOrderQuery.VendorRef = this.PreviousPage.VendorRef;
                    purchaseOrderQuery.ExternalPoID = this.PreviousPage.ExternalPoID;
                }
                else
                {
                    purchaseOrderQuery.PoID = "";
                }
            }
            else
            {
                this.hdnFrom.Value = base.Request.QueryString["from"].ToString();
            }
            purchaseOrderQuery.IsGetAll = false;
            purchaseOrderQuery.CurrentPage = "0";
            purchaseOrderQuery.PageSize = this.gvPurchaseOrder.PageSize.ToString();
            purchaseOrderQuery.SortField = "PO_ID";
            purchaseOrderQuery.SortDirection = "DESC";
            this.PagerFooter.PageSize = this.gvPurchaseOrder.PageSize;
            if (!string.IsNullOrEmpty(base.Request.QueryString["action"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["action"].ToString();
                this.btnFinish.Visible = false;
                this.gvPurchaseOrder.Columns[1].Visible = false;
                this.gvPurchaseOrder.Columns[2].Visible = true;
                this.btnNew.Visible = false;
                this.btnEdit.Visible = false;
                this.btnDelete.Visible = false;
                this.btnAction.Visible = false;
                this.btnReSearch.Visible = false;
                this.btnExport.Visible = false;
                this.btnReport.Visible = false;
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.hdnActionID.Value = base.Request.QueryString["export"];
            }
            this.SetPurchaseOrderQueryEntity(purchaseOrderQuery);
            this.BindData2PurchaseOrderList(purchaseOrderQuery);
            if (!string.IsNullOrEmpty(base.Request.QueryString["export"]))
            {
                this.btnExport_Click(null, null);
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!base.IsPostBack)
            {
                base.InitPage(this.Page);
                this.LoadData();
            }
        }

        private void SetPurchaseOrderQueryEntity(PurchaseOrderQueryEntity purchaseOrderQuery)
        {
            if (!string.IsNullOrEmpty(this.hdnActionID.Value))
            {
                base.PageQueryEntityViewState = purchaseOrderQuery;
            }
            else
            {
                base.PageQueryEntitySession = purchaseOrderQuery;
            }
        }

        public PurchaseOrderQuery PreviousPage
        {
            get
            {
                return (PurchaseOrderQuery) base.PreviousPage;
            }
        }
    }
}

